In article <4i4k40$qa6@news.ysu.edu>, Bryan Kelly <bp515@yfn.ysu.edu> wrote:
>
>I am relitivly new to programming in C. I have a program that need to get random numbers. Does anyone know how to do this. I alread tried the rand() function. Please E-Mail me at BillsFan10@aol.com if you know. Thanx.
>--
>Bryan "BillsFan" Kelly
Why not use rand? In case you had trouble using it, here are some examples:
In this first example the same "default" sequence of random numbers is
generated every time.
#include <stdio.h>
#include <stdlib.h>
int main () {
int k, rr;
for (k=1; k<=10; k++) {
rr = rand();
printf(" %6d\n", rr);
}
return 0;
}
In the second example srand is used to initialize rand. The same sequence
of random numbers is generated each time, but it is a different sequence
than the default sequence (unless the parameter to srand is 1).
#include <stdio.h>
#include <stdlib.h>
int main () {
int k, rr;
srand(777);
for (k=1; k<=10; k++) {
rr = rand();
printf(" %6d\n", rr);
}
return 0;
}
Finally, time is used to get a value to initialize rand so that a
different sequence is generated each time the program is run.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main () {
int k, rr;
srand(time(NULL));
for (k=1; k<=10; k++) {
rr = rand();
printf(" %6d\n", rr);
}
return 0;
}
There is further information about random numbers in the FAQ for